package com.www.project.meetschool.controller;

import com.www.common.utils.SecurityUtils;
import com.www.framework.aspectj.lang.annotation.Log;
import com.www.framework.aspectj.lang.enums.BusinessType;
import com.www.framework.web.controller.BaseController;
import com.www.framework.web.domain.AjaxResult;
import com.www.framework.web.page.TableDataInfo;
import com.www.project.meetschool.domain.MeetEvent;
import com.www.project.meetschool.domain.Message;
import com.www.project.meetschool.service.MeetEventService;
import com.www.project.monitor.domain.MeetEventUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动管理
 *
 * @author www
 */
@RestController
@RequestMapping("/meet/event")
public class MeetEventController extends BaseController
{

     @Autowired
     private MeetEventService meetEventService;

    /**
     * 获取活动列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MeetEvent event)
    {
        startPage();
        List<MeetEvent> list = meetEventService.selectEventList(event);

        return getDataTable(list);
    }

    /**
     * 获取活动列表
     */
    @GetMapping("/message/list")
    public TableDataInfo messageList()
    {
        startPage();
        Message message = new Message();
        message.setConsumerId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<Message> list = meetEventService.selectMessageList(message);

        return getDataTable(list);
    }

    /**
     * 获取活动列表
     */
    @GetMapping("/listToken")
    public TableDataInfo list1(MeetEvent event)
    {
       TableDataInfo dataInfo = list(event);
       List<MeetEvent> events = (List<MeetEvent>) dataInfo.getRows();
        events.forEach(l->{
            if(!l.getUsers().isEmpty() &&  SecurityUtils.getLoginUser().isCredentialsNonExpired()){
                Boolean flag =  l.getUsers().contains(SecurityUtils.getLoginUser().getUser().getUserId());
                l.setFlag(flag);
            }
        });
        return  dataInfo;
    }

      @Log(title = "活动管理", businessType = BusinessType.INSERT)
      @PostMapping
      public AjaxResult add(@RequestBody MeetEvent event)
      {
          event.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
          event.setCreateBy(SecurityUtils.getUsername());
          return  toAjax(meetEventService.add(event));
      }

    /**
     * 修改活动
     */
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeetEvent event)
    {

        event.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(meetEventService.updateEvent(event));
    }




    @GetMapping(value = { "/{eventId}" })
    public AjaxResult getInfo(@PathVariable Long eventId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, meetEventService.selectEventById(eventId));
        return ajax;
    }



    /**
     * 删除活动
     */
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{eventIds}")
    public AjaxResult remove(@PathVariable Long[] eventIds)
    {
        return toAjax(meetEventService.deleteEventByIds(eventIds));
    }



    /**
     * 加入活动
     */
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping("/join")
    public AjaxResult joinEvent(@RequestBody MeetEventUser eventUser)
    {
        if(!meetEventService.checkPersonNumber(eventUser))
        {
            return  AjaxResult.error(202,"活动人数已满");
        }
        if(!meetEventService.checkStatus(eventUser))
        {
            return  AjaxResult.error(202,"项目已经开始或结束");
        }
        eventUser.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return toAjax(meetEventService.joinEvent(eventUser));

    }


    /**
     * 退出活动
     */
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @PostMapping("/exit")
    public AjaxResult exitEvent(@RequestBody MeetEventUser eventUser)
    {
        if(!meetEventService.checkStatus(eventUser))
        {
            return  AjaxResult.error(202,"项目已经开始或结束");
        }
        eventUser.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return toAjax(meetEventService.exitEvent(eventUser));
    }


}
